<template>
    <div id="box">
    <!--面包屑区域-->
    <el-breadcrumb>
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>添加商品</el-breadcrumb-item>
</el-breadcrumb>
<!--卡片视图区域-->
<el-card>
    <!--警告-->
    <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>
    <!--步骤条-->
    <el-steps :space="200" :active="actioveIndex / 1" finish-status="success" align-center>
  <el-step title="基本信息"></el-step>
  <el-step title="商品参数"></el-step>
  <el-step title="商品属性"></el-step>
  <el-step title="商品图片"></el-step>
  <el-step title="商品内容"></el-step>
  <el-step title="完成"></el-step>
</el-steps>
<!--标签页-->
<el-form :model="addruleForm" :rules="addrules" ref="addruleForm" label-width="100px" label-position="top">
<el-tabs v-model="actioveIndex" :tab-position="'left'" :before-leave="beforeTableacy" @tab-click="tabClicked">
    <el-tab-pane label="基本信息" name="0">
        <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="addruleForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="addruleForm.goods_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="addruleForm.goods_weight" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="addruleForm.goods_number" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="goods_cat">
            <el-cascader
    v-model="addruleForm.goods_cat"
    :options="catelist" :props="{ expandTrigger: 'hover', label: 'cat_name', value: 'cat_id', children: 'children' }"
    @change="handleChange"></el-cascader>
        </el-form-item>
    </el-tab-pane>
    <el-tab-pane label="商品参数" name="1">
         <el-form-item :label="item.attr_name" v-for="(item, index) in manyTableData" :key="item.attr_id">
            <el-checkbox-group v-model="item.attr_vals">
    <el-checkbox :label="item1" v-for="(item1, index1) in item.attr_vals" :key="index1" border></el-checkbox>
    </el-checkbox-group>
            </el-form-item>
    </el-tab-pane>
    <el-tab-pane label="商品属性" name="2">
        <el-form-item :label="item.attr_name" v-for="(item, index) in onlyTableData" :key="item.attr_id">
        <el-input v-model="item.attr_vals"></el-input>    
        </el-form-item>
    </el-tab-pane>
    <el-tab-pane label="商品图片" name="3">
        <el-upload
  action="https://lianghj.top:8888/api/private/v1/upload"
  :headers="headersobj"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  list-type="picture" :on-success="handleSucess">
  <el-button size="small" type="primary">点击上传</el-button>
</el-upload>
    </el-tab-pane>
    <el-tab-pane label="商品内容" name="4">
        <quill-editor v-model="addruleForm.goods_introduce"></quill-editor>
        <el-button type="primary" class="btnAdd" @click="add()">添加商品</el-button>
    </el-tab-pane>
  </el-tabs>
</el-form>
    </el-card>
    <!--图片预览对话框-->
    <el-dialog
  title="图片预览"
  :visible.sync="picVisible"
  width="50%"
  >
  <img :src="previewurl" alt="" width="100%">
</el-dialog>
    </div>
    </template>

<script>
export default {
    data() {
        return {
            //步骤条激活
            actioveIndex: '0',
            //添加表单数据
            addruleForm: {
                goods_name: '',
                goods_price: 0,
                goods_weight: 0,
                goods_number: 0,
                goods_cat: [],
                pics: [],
                goods_introduce: ''
            },
            //验证
            addrules: {
                goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
                goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
                goods_weight: [{ required: true, message: '请输入商品重量', trigger: 'blur' }],
                goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }],
                goods_cat: [{ required: true, message: "请选择分类列表", trigger: "blur" }]
            },
            //商品分类数据
            catelist: [],
            //动态参数数据
            manyTableData: [],
            //静态属性数据
            onlyTableData: [],
            //设置图片上传请求头
            headersobj: {
                Authorization: window.sessionStorage.getItem("login")
            },
            //图片预览路径
            previewurl: '',
            //图片对话框的显示与隐藏
            picVisible: false
        }
    },
    created: function () {
        this.getCateList()

    },
    methods: {
        //获取所有分类数据
        async getCateList() {
            let { data } = await this.$http.get("categories")
            // console.log(data)
            if (data.meta.status !== 200) return this.$message.error("商品分类数据失败")
            this.catelist = data.data
        },
        //多级选项改变
        handleChange() {
            if (this.addruleForm.goods_cat.length !== 3) {
                return this.addruleForm.goods_cat = []
            }
        },
        //标签页切换时
        beforeTableacy(to, from) {
            // console.log(to,from)
            if (from === '0' && this.addruleForm.goods_cat.length !== 3) {
                this.$message.error("请先选择商品分类")
                return false
            }
        },
        //获取动态参数数据
        async getParamalist1() {
            let { data } = await this.$http.get(`categories/${this.addruleForm.goods_cat[2]}/attributes`, { params: { sel: 'many' } })
            // console.log(data)
            if (data.meta.status !== 200) return this.$message.error("动态参数获取失败")
            data.data.forEach((item) => {
                item.attr_vals = item.attr_vals.length === 0 ? [] : item.attr_vals.split(",")
            })
            this.manyTableData = data.data
        },
        //获取静态属性数据
        async getParamalist2() {
            let { data } = await this.$http.get(`categories/${this.addruleForm.goods_cat[2]}/attributes`, { params: { sel: 'only' } })
            // console.log(data)
            if (data.meta.status !== 200) return this.$message.error("静态属性获取失败")
            this.onlyTableData = data.data

        },
        //点击标签页切换
        tabClicked() {
            if (this.actioveIndex === '1') {
                this.getParamalist1()
            } else if (this.actioveIndex == "2") {
                this.getParamalist2()
            }
        },
        //点击上传的图片
        handlePreview(file) {
            // console.log(file)
            this.previewurl = file.response.data.url
            this.picVisible = true
        },
        //点击差号
        handleRemove(file) {
            // console.log(file)
            let removea = file.response.data.tmp_path
            let i = this.addruleForm.pics.findIndex((x) => {
                x.pic === removea
            })
            this.addruleForm.pics.splice(i, 1)

        },
        //上传成功后
        handleSucess(res) {
            // console.log(res)
            let picinfo = { pic: res.data.tmp_path }
            this.addruleForm.pics.push(picinfo)
            // console.log(this.addruleForm)
        },
        //点击添加商品
        add() {
            // console.log(this.addruleForm)
            this.$refs.addruleForm.validate(async (bool) => {
                if (!bool) return this.$message.error("请填写必要的表单项")
                this.addruleForm.goods_cat = this.addruleForm.goods_cat.join(",")
                let { data } = await this.$http.post("goods", this.addruleForm)
                // console.log(data)
                if (data.meta.status !== 201) return this.$message.error("添加失败")
                this.$message.success("添加成功")
                this.$router.push("/goods")
            })
        }

    }
}
</script>

<style lang="less" scoped>
.el-checkbox {
    margin: 0 5px 0 5px !important
}

.btnAdd {
    margin-top: 15px;
}
</style>